package com.chenjt.netty.eight_nio.zeroCopy;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/8/22 9:37
 */
public class FileChannelMap {

    public static void main(String[] args) throws Exception {
        // rw是赋予读写权限
        RandomAccessFile randomAccessFile = new RandomAccessFile("src/main/java/com/chenjt/netty/eight_nio/zeroCopy/fileChannelMap.txt", "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();

        // 将文件内容映射到 MappedByteBuffer中，权限是读和写，通过操作MappedByteBuffer达到操作文件指定的内容
        MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 5);

        // 将 FileChannelMap.txt 中的第0和第3个字符换成a、b
        mappedByteBuffer.put(0,(byte)'a');
        mappedByteBuffer.put(3,(byte)'b');

        randomAccessFile.close();

    }


}
